package com.lxm.shownews.utils;

import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by lxm on 2016/2/26.
 */
public class DateUtils {


    private static SimpleDateFormat DATE_FORMAT_TILL_SECOND = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    private static SimpleDateFormat DATE_FORMAT_HOUR_MINUTE = new SimpleDateFormat("mm:ss");

    /**
     * 日期转为字符窜
     *
     * @param date
     * @param format
     * @return
     */
    public static String dateToStr(Date date, String format) {
        if (date != null) {
            DateFormat df = new SimpleDateFormat(format);
            return df.format(date);
        }
        return "";
    }

    /**
     * 日期转为字符窜
     *
     * @param date
     * @param sdf
     * @return
     */
    public static String dateToStr(Date date, SimpleDateFormat sdf) {
        if (date != null && sdf != null) {
            return sdf.format(date);
        }
        return "";
    }

    /**
     * 日期字符窜转换为Date
     *
     * @param dateStr
     * @param format
     * @return
     */
    public static Date strToDate(String dateStr, String format) {
        Date date = null;
        if (!TextUtils.isEmpty(dateStr)) {
            DateFormat df = new SimpleDateFormat(format);
            try {
                date = df.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }

    /**
     * 日期字符窜转换为Date
     *
     * @param dateStr
     * @param sdf
     * @return
     */
    public static Date strToDate(String dateStr, SimpleDateFormat sdf) {
        Date date = null;
        if (!TextUtils.isEmpty(dateStr) && sdf != null) {
            try {
                date = sdf.parse(dateStr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return date;
    }


    /**
     * 获取Date的当前年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.YEAR);
        }
        return 0;
    }

    /**
     * 获取Date的当前月份
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.MONTH) + 1;
        }
        return -1;
    }

    /**
     * 获取Date的DAY_OF_MONTH在某月的第几天
     *
     * @param date
     * @return
     */
    public static int getDay(Date date) {
        if (date != null) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            return calendar.get(Calendar.DAY_OF_MONTH);
        }
        return 0;
    }


    /**
     * 是否是同一年
     *
     * @param date
     * @return
     */
    public static boolean isSameYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        int nowYear = calendar.get(Calendar.YEAR);
        calendar.setTime(date);
        int postYear = calendar.get(Calendar.YEAR);
        if (nowYear == postYear)
            return true;
        return false;
    }

    /**
     * 是否同一年的同一个月
     *
     * @param date
     * @return
     */
    public static boolean isSameMonth(Date date) {
        if (!isSameYear(date))
            return false;
        Calendar calendar = Calendar.getInstance();
        int nowMonth = calendar.get(Calendar.MONTH) + 1;
        calendar.setTime(date);
        int postMonth = calendar.get(Calendar.MONTH) + 1;
        if (nowMonth == postMonth)
            return true;
        return false;
    }

    /**
     * 是否同一年同一个月的同一天
     *
     * @param date
     * @return
     */
    public static boolean isSameDay(Date date) {
        if (!isSameMonth(date))
            return false;
        Calendar calendar = Calendar.getInstance();
        int nowDay = calendar.get(Calendar.DAY_OF_MONTH);
        calendar.setTime(date);
        int postDay = calendar.get(Calendar.DAY_OF_MONTH);
        if (nowDay == postDay)
            return true;
        return false;
    }

    /**
     * 时间显示逻辑
     * @param dateStr
     * @return
     */
    public static String timeLogicShow(String dateStr) {
        Calendar calendar = Calendar.getInstance();
        int i = calendar.get(Calendar.DAY_OF_MONTH);

        long now = calendar.getTimeInMillis();
        Date date = strToDate(dateStr, DATE_FORMAT_TILL_SECOND);
        calendar.setTime(date);
        long post = calendar.getTimeInMillis();

        //相差的秒数
        long time = (now - post) / 1000;
        StringBuffer sb = new StringBuffer();
        if (time >= 0 && time < 60) {//1分钟内
            return sb.append("刚刚").toString();
        } else if (time >= 60 && time < 60 * 60) {//1小时内
            return sb.append(time / 60 + "分钟前").toString();
        } else if (time >= 60 * 60 && time < 60 * 60 * 24) {//24小时内
            return sb.append(time / 60 / 60 + "小时前").toString();
        } else if (time >= 60 * 60 * 24 && time < 60 * 60 * 24 * 2) {//昨天
            return sb.append("昨天" + dateToStr(date, DATE_FORMAT_HOUR_MINUTE)).toString();
        } else if (time >= 60 * 60 * 24 * 2 && time < 60 * 60 * 24 * 3) {//前天
            return sb.append("前天" + dateToStr(date, DATE_FORMAT_HOUR_MINUTE)).toString();
        } else {
//            if (!isSameYear(date)) { //不是同一年
//                return sb.append(dateToStr(date, DATE_FORMAT_TILL_SECOND)).toString();
//            }
//            if(isSameYear(date) && !isSameMonth(date)){//同一年不是同一月
//                return sb.append()
//            }
            return sb.append(dateToStr(date, DATE_FORMAT_TILL_SECOND)).toString();
        }

    }


}


